﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using AssetsSys.BLL;
using AssetsSys.Dto.Assets;
using AssetsSys.Dto.Department;
using AssetsSys.Dto.Permission;
using AssetsSys.WebApi.Filters;
using AssetsSys.WebApi.Models.Auth;
using AssetsSys.WebApi.Models.Department;
using AssetsSys.WebApi.Models.Permission;
using AssetsSys.WebApi.Models.Source;

namespace AssetsSys.WebApi.Controllers
{
    /// <summary>
    /// 部门管理
    /// </summary>
    [MyAuth]
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    [RoutePrefix("api/department")]
    public class DepartmentController : ApiController
    {
        /// <summary>
        /// 部门列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("infos")]
        public async Task<IHttpActionResult> GetDepartmentList()
        {
            return this.SendData(await DepartmentManger.GetList());
        }

        /// <summary>
        /// 部门列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("noadmininfos")]
        public async Task<IHttpActionResult> GetDepartmentNoAdminList()
        {
            var user = await UserManger.GetById(((UserIdentity)User.Identity).Id);
            return this.SendData(await DepartmentManger.GetNoAdminList(user.DepartmentId));
        }

        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("infos")]
        public async Task<IHttpActionResult> CreateDepartment(CreateDepartmentViewModel model)
        {
            if (!ModelState.IsValid) return this.ErrorData(ModelState.Values.FirstOrDefault()?.Errors[0].ErrorMessage);
            return await DepartmentManger.Create(model.DepartmentName,model.PermissionId,Guid.NewGuid()) ? this.SendData("添加成功") : this.ErrorData("添加失败");
        }

        /// <summary>
        /// 获取部门下的用户
        /// </summary>
        /// <param name="departmentid">部门Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{departmentid}/list")]
        public async Task<IHttpActionResult> GetDepartmentUers(Guid departmentid)
        {
            return this.SendData(await UserManger.GetDepartmentUserList(departmentid));
        }

        /// <summary>
        /// 我的部门资产列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("assets/list")]
        public async Task<IHttpActionResult> GetMyDepartmentAssetsList([FromUri]AssetsQuertDto request)
        {
            request.UserId = ((UserIdentity)User.Identity).Id;
            var data = await AssetsManger.GetMyDepartmentAssetsList(request);
            return this.SendData(data);
        }

        /// <summary>
        /// 获取部门信息
        /// </summary>
        /// <param name="departmentid">部门Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{departmentid}/infos")]
        public async Task<IHttpActionResult> GetDepartment(Guid departmentid)
        {
            return this.SendData(await DepartmentManger.GetById(departmentid));
        }

        /// <summary>
        /// 更新部门名称
        /// </summary>
        /// <param name="departmentid">部门Id</param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("{departmentid}/infos")]
        public async Task<IHttpActionResult> UpdateDepartment(Guid departmentid, DepartmentModel model)
        {
            model.Id = departmentid;
            return await DepartmentManger.Update(model) ? this.SendData("修改成功") : this.ErrorData("修改失败");
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="departmentid">部门Id</param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{departmentid}/infos")]
        public async Task<IHttpActionResult> DeleteDepartment(Guid departmentid)
        {
            return await DepartmentManger.Delete(departmentid) ? this.SendData("删除成功") : this.ErrorData("删除失败");
        }
    }
}
